package com.qualityevaluationclouduser.controller;

import com.qualityevaluationclouduser.entity.Empower;
import com.qualityevaluationclouduser.entity.RoleInfo;
import com.qualityevaluationclouduser.service.RoleInfoService;
import com.qualityevaluationclouduser.untils.ResultEntity;
import com.qualityevaluationclouduser.untils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: qualityEvaluationCloudProject
 * @description: 角色信息控制器
 * @author:
 * @create:
 **/
@RequestMapping("/roleInfoController")
@RestController
@Api(tags = "角色信息")
public class RoleInfoController {
    @Autowired
    private RoleInfoService roleInfoService;

    @ApiOperation(value = "新增角色信息", notes = "新增角色信息")
    @PostMapping("/add")
    public ResultEntity addRoleInfo(@RequestBody RoleInfo roleInfo) {
        return ResultUtils.build(roleInfoService.addRoleInfo(roleInfo));
    }

    @ApiOperation(value = "删除角色信息", notes = "删除角色信息")
    @DeleteMapping("/delete/{id}")
    public void delRoleInfo(@PathVariable Long id) {
        roleInfoService.delRoleInfo(id);
    }

    @ApiOperation(value = "更新角色信息", notes = "更新角色信息")
    @PutMapping("/update")
    public ResultEntity updRoleInfo(@RequestBody RoleInfo roleInfo) {
        return ResultUtils.build(roleInfoService.updRoleInfo(roleInfo));
    }

    @ApiOperation(value = "分页查询角色信息", notes = "分页查询角色信息")
    @GetMapping("/page")
    public ResultEntity pageQueryRoleInfoList(@RequestParam int page, @RequestParam int size) {
        return ResultUtils.build(roleInfoService.pageQueryRoleInfoList(page  , size));
    }

    @ApiOperation(value = "根据名称分页查询角色信息",notes = "根据名称分页查询角色信息")
    @GetMapping("/specificPage")
    public ResultEntity pageQueryRoleInfoByName(String name,@RequestParam int page,@RequestParam int size)
    {
        return ResultUtils.build(roleInfoService.pageQueryRoleInfoByName(name,page,size));
    }

    @ApiOperation(value = "批量删除",notes = "批量删除")
    @DeleteMapping("/batch")
    public void batchDelete(@RequestParam String idString) {
        String[] split = idString.split(",");
        List<Long> ids = new ArrayList<>();
        for (String id : split
        ) {
            ids.add(Long.parseLong(id));
        }
        roleInfoService.delRoleInfosByIdIn(ids);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取所有权限信息",notes = "获取所有权限信息")
    public ResultEntity findRoleInfoList()
    {
        return ResultUtils.build(roleInfoService.findRoleInfoList());
    }

    @PostMapping("/empower")
    @ApiOperation("给角色绑定多个权限")
    public void addDefaultAuthoritys(Empower empower)
    {
        roleInfoService.addDefaultAuthoritys(empower.getRoleId(),empower.getMenuInfoIdList());
    }
}
